/**
 * SpecialProductBean.java
 *
 * Created on 13 novembre 2007, 16:28
 */
package com.reimscopie.beans.entities;

import java.util.* ;

import javax.persistence.* ;

import com.reimscopie.* ;


/**
 * This entity represents a Special product, only for resale.
 */
@Entity
@NamedQueries({
  @NamedQuery(
    name  = "all-special-products",
    query = "SELECT o FROM SpecialProductBean o ORDER BY name"
  ),
  @NamedQuery(
    name  = "all-valid-special-products",
    query = "SELECT o FROM SpecialProductBean o WHERE valid=true ORDER BY name")
})

public class SpecialProductBean extends ProductBean<SpecialProduct> {
  
  // The price of this special product
  private float price ;
  
  // The current stock of thi special product
  private int stock ;
  
  // The list of choice fot this special product
  private String[] choices ;
  
  
  /**
   * Create a new instance of <code>SpecialProductBean</code>
   * (default constructor).
   */
  public SpecialProductBean() {
  }
  
  
  /**
   * Create a new instance of <code>SpecialProductBean</code> with all
   * attributes.
   *
   * @param specialProduct The valued SpecialProduct.
   */
  public SpecialProductBean(SpecialProduct specialProduct) {
    
    if (specialProduct.hasId())
      throw new IllegalArgumentException("special-product has an image") ;
    
    setName(specialProduct.getName()) ;
    setDescription(specialProduct.getDescription()) ;
    setValid(specialProduct.isValid()) ;
    setPicture(specialProduct.getPicture()) ;
    
    this.price   = specialProduct.getPrice() ;
    this.stock   = specialProduct.getStock() ;
    this.choices = specialProduct.getChoices() ;
    
  }
  
  
  /**
   * Return the price of this special product.
   *
   * @return The price of this special product
   */
  public float getPrice() {
    return price ;
  }
  
  
  /**
   * Set the price of this special product.
   *
   * @param price The price of this special product.
   */
  public void setPrice(float price) {
    this.price = price ;
  }
  
  
  /**
   * Return the current stock of this special product.
   *
   * @return The current stock of this special product.
   */
  public int getStock() {
    return stock ;
  }
  
  
  /**
   * Set the current stock for this product.
   *
   * @param stock The current stock for this product.
   */
  public void setStock(int stock) {
    this.stock = stock ;
  }
  
  
  /**
   * Return the choicies list of this product.
   *
   * @return The choices list.
   */
  public String[] getChoices() {
    return choices ;
  }
  
  
  /**
   * Set the choicies of this product.
   *
   * @param choices The new choices.
   */
  public void setChoices(String[] choices) {
    this.choices = choices ;
  }
  
  /**
   * Transform this entity bean into the associated value.
   * 
   * @return A new value instance.
   */
  @Override
  public SpecialProduct toValue() {
    
    Collection<CommentBean> collection = getComments() ;
    CommentBean[] beans = new CommentBean[collection.size()] ;
    collection.toArray(beans) ;
    
    Comment[] comments = new Comment[beans.length] ;
    for (int i=0;i<beans.length;i++)
      comments[i] = beans[i].toComment() ;
    
    return new SpecialProduct(
        getId(),
        getName(),
        getDescription(),
        getPicture(),
        getValid(),
        comments,
        getPrice(),
        getStock(),
        getChoices()
    ) ;
  }
}
